
// <editor-fold defaultstate="collapsed" desc="import">
package com.compuvest.ajax;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.compuvest.web.SQLBean;
import com.compuvest.web.Mail;
// </editor-fold>

public class DropPriceAlert extends HttpServlet {

    private SQLBean mysql;
    private Mail mail;

// <editor-fold defaultstate="collapsed" desc="init()">

    @Override
    public void init(ServletConfig config) throws ServletException {
	super.init(config);

        mysql = (SQLBean)config.getServletContext().getAttribute("sb");
	if(mysql == null) {
            System.out.println("DropPriceAlert Error sb==null:");
        }
        mail = (Mail)config.getServletContext().getAttribute("mail");
	if(mail == null) {
            System.out.println("DropPriceAlert Error mail==null:");
        }
        System.out.println("DropPriceAlert init:");
    }

// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="processRequest()">

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        String action = request.getParameter("act");
	if(action != null) {
            String itemID = request.getParameter("iid");
            if("it".equals(action)) { //

		PrintWriter out = response.getWriter();
		try {
                    String json = mysql.getItemDropPriceAlert(itemID);
                    out.print(json);
                }
		finally {
                    out.close();
		}
            }
            else if("sd".equals(action)) { // send email

		mail.sendDropPrice(
                    itemID,
                    request.getParameter("name"),
                    request.getParameter("phone"),
                    request.getParameter("email")
                );
            }
            else if("qr".equals(action)) { // quote request (OnSiteServices.jsp)
		mail.quoteRequest(request.getParameter("ms"));
            }
	}
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods.">
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    public String getServletInfo() {
        return "DropPriceAlert";
    }
    @Override
    public void destroy() {
        super.destroy();
        mysql = null;
        mail = null;
	System.out.println("DropPriceAlert Destroy:");
    }
// </editor-fold>

}
